<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('商品库')" />
	<th:block th:include="include :: businessCSS" />
	<style>

		.preservationBtn{padding: 10px;text-align: center;}
		.preservationBtn input{font-size: 16px;background-color: rgb(80, 138, 238);color: #fff;border: none;padding: 5px 30px;border-radius: 10px;cursor: pointer;}

	</style>

</head>

<body class="no-skin">
<div class="main-container" id="main-container">
	<div class="main-content">
	<form class="layui-form head-form">
	    <div class="north" style="background-color: #fff;">
			<div class="forminput"></div>
			<div class="layui-form-item preservationBtn">
				<input th:if="${type == '1'}" type="button" lay-submit lay-filter="btn-edit" id="btn-edit" value="保存">
				<input type="button" lay-submit lay-filter="close" id="close" value="关闭">
			</div>
    	</div>
    </form>
	</div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: businessJS" />
<!-- <th:block th:include="include :: inlineJS" /> -->

<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "base/logsticsserver";
	var	k_fields=[]
	var fields = [[${fields}]];
	var fieldsList = [[${fieldsList}]];
	var fieldsHiddenList = [[${fieldsHiddenList}]];
	var gridColumns= [[${gridColumns}]];
	var type= [[${type}]];
	var all_fields= fieldsList;
	var $,layer,form,table;
    layui.use(['laydate','form','layer','jquery','util','element','upload'], function () {
        $ = layui.$,layer = layui.layer,form = layui.form,laydate = layui.laydate,util = layui.util,element=layui.element,upload = layui.upload;
		type == 1?headcom():headcom1();
		form.on('submit(btn-edit)', function(data){
		    var is_return = false//是否中断保存
            fieldsList.map((v,i)=>{
                if(v.fieldFrontRequired){
                    if(!$('#'+v.fields).val()){
                         layer.msg(v.fieldName+'不能为空')
                         is_return=true
                    }
                }
            })
            if(is_return) return;
			$.modal.loading("正在处理中，请稍后...");
		    $.post("/base/logsticsserver/editpost",data.field,function(res){
				layer.msg(res.msg);
				if(res.code == 0){
					setTimeout(function(){
						window.location.href = '/base/logsticsserver/edit?guid=' + res.data+'&type=1';
					}, 1000);
				}
	        });
            return false;
       });

		//关闭
		form.on('submit(close)', function(data){
			top.layui.admin.closeThisTabs();
		});


	    $(".fieldsimg").each(function(){
	    	var id=$(this).attr('name')
            upload.render({
                elem: '#'+id+'_upimg',
	            url: '/system/fileinfo/uploadmailimg',
	            accept: 'images',
	    		before: function(obj){
                    //预读本地文件示例，不支持ie8
                    obj.preview(function(index, file, result){
                      console.log(index, file, result)
                    });
                },
	            done: function (res) {
                    //如果上传失败
                    if (res.errno > 0) {
                        return layer.msg('上传失败');
                    }else{
	    				layer.msg('上传成功');
	    				$('#'+id).val(res.data[0])
	    				layui.$('#'+id+'_upimg').find('.upicon').addClass('layui-hide')
	    				layui.$('#'+id+'_upimg').find('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.data[0]);
	    			}
                },error: function (err) {
                    //演示失败状态，并实现重传
	            	console.log(err)
                }
            });
        })


		$('.t_date').each(function(){
            var datetype=$(this).attr('datetype')
            laydate.render({
				elem:this
                ,type: datetype=='t_date'?'date':datetype=='t_datetime'?'datetime':datetype=='t_year'?'year':'month'
			})
		})


		// beforeInit();//表单初始化之前的动作集合
		// layui.element.render();
		// 物流服务商详情表头
		function headcom1(){
			var str=`<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<div class="layui-form-item margin_t0">`
			$.each(fieldsList,function(i,v){
				str+=`<div class="layui-inline margin_r0 ${v.fieldFrontTopSize == 1 ?'layui-col-xs4':(v.fieldFrontTopSize == 2 ?'layui-col-xs8':'layui-col-xs12')}">
					<label class="layui-form-label">`
				if(v.fieldFrontRequired){str+=`<font class="f_red">*</font>`}
				str+=`<font>${v.fieldName}：</font>
					</label>
					<div class="layui-input-block">`

				if(v.fieldFrontType=='t_file2'){
					str+=`<div  name="${v.fields}" id="${v.fields}_upimg">
	    					<input type="hidden" id="${v.fields}" name="${v.fields}"/>
	    					<div class="${v.fieldContent ?'' :'layui-hide'}" id="uploadDemoView">
	    					  <img src="${v.fieldContent}" alt="上传成功后渲染" style="max-width: 196px">
	    					</div>
	    				</div>`
				} else {
					str+=`<input  type="text"  name="${v.fields}" id="${v.fields}" value="${v.fieldContent}" class="${v.fieldFrontEdit ?'layui-input canntEdit':'layui-input canntEdit'}" lay-verify="${v.fieldFrontRequired ?'required':''}" autocomplete="off" onblur="${v.fieldFrontRequired ?'inputOnBlur(this)':''}" ${v.fieldFrontEdit?'readonly':'readonly'}>`
				}
				str+=`</div></div>`

			})
			str+=`</div></div>`

			// $.each(fieldsHiddenList,function(i,v){
				// str+=`<input type="hidden" name="${v.fields}" id="${v.fields}" value="${v.fieldContent}">`
			// })
			$('.forminput').html(str)
			form.render();
		}
    });

	/*]]>*/
</script>

</body>
</html>